<template>
	<el-dialog v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="80px">
			<el-form-item label="结算金额" prop="patientMoney">
				<el-input v-model="dataForm.patientMoney" type="number"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitMoney()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { usePatientMoneySubmitApi } from '@/api/sys/patientmoney'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	id:0,
	patientMoney: 0,
	orgId:'',
	patientId:'',
	isSettle:1,
	totalPrice:0
})

const init = (row: any) => {
	console.log(row);
	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}
	visible.value = true;
	dataForm.id=row.id;
	let orgid:any=localStorage.getItem('org');
	dataForm.orgId=orgid;
	dataForm.patientId=row.patientId;
	dataForm.totalPrice=row.patientMoney;
	// if(dataForm.totalPrice>dataForm.patientMoney){
	// 	dataForm.isSettle=0;
	// }
}
// 提交药品
const submitMoney = () => {
	if(dataForm.totalPrice>dataForm.patientMoney){
		ElMessage.error('结算金额不足！')
		return;
	}
	usePatientMoneySubmitApi(dataForm).then(() => {
		ElMessage.success({
			message: '操作成功',
			duration: 500,
			onClose: () => {
				visible.value = false
				emit('refreshDataList')
			}
		})
	})
}
const dataRules = ref({
	patientMoney: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
})
defineExpose({
	init
})
</script>
